﻿using System;
using NetSurveillance.WebApp.DataServices.Models;
using ServiceStack.OrmLite;

namespace NetSurveillance.WebApp.DataServices.Implementations
{
    public class ProtocolService : BaseService<Protocol>
    {
       public ProtocolService(string connStr, IOrmLiteDialectProvider provider = null)
            : base(connStr, provider)
        {
        }

       public ServiceResult SaveAllProtocols(string[] protocolArr)
       {
           using(var db = DbFactory.OpenDbConnection())
           {
               db.Update<Protocol>(new {StatusToken = 0}, p => p.StatusToken == 1);
               if(protocolArr != null)
                   foreach (var protocol in protocolArr)
                   {
                       var protocolEntry = db.SingleWhere<Protocol>("ProtocolType", protocol);
                       if (protocolEntry == null)
                           db.Insert(new Protocol {ProtocolType = protocol});
                       else
                       {
                           protocolEntry.StatusToken = 1;
                           protocolEntry.LastModifiedTime = DateTime.Now;
                           db.Update(protocolEntry);
                       }
                   }
           }
           return ServiceResult.SuccessResult;
       }
    }
}
